package me.zhengjie.modules.system.service.impl;

import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.system.domain.SafeRiskCase;
import me.zhengjie.modules.system.repository.SafeRiskCaseRepository;
import me.zhengjie.modules.system.service.SafeTestCaseService;
import me.zhengjie.modules.system.service.dto.SafeRiskCaseDto;
import me.zhengjie.modules.system.service.mapstruct.SafeRiskCaseMapper;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * SafeRiskTypeServiceImpl
 *
 * @author wj
 */
@Service
@RequiredArgsConstructor
@CacheConfig(cacheNames = "safeTestCase")
public class SafeTestCaseServiceImpl implements SafeTestCaseService {

    private final SafeRiskCaseRepository safeRiskCaseRepository;
    private final SafeRiskCaseMapper safeRiskCaseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(SafeRiskCase entity) {
        safeRiskCaseRepository.save(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String id) {
        safeRiskCaseRepository.deleteById(id);
    }

    @Override
    public List<SafeRiskCaseDto> queryAll(String sceneType) {
        final List<SafeRiskCase> bySceneType = safeRiskCaseRepository.findAll();
        return safeRiskCaseMapper.toDto(bySceneType);
    }

    @Override
    public List<SafeRiskCase> queryByCaseId(String id) {
        return safeRiskCaseRepository.findAllById(Collections.singletonList(id));
    }

    @Override
    public List<SafeRiskCase> queryByCaseIds(String ids) {
        String[] idss = ids.split(",");
        return safeRiskCaseRepository.findAllById(new ArrayList<>(Arrays.asList(idss)));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(SafeRiskCase entity) {
        safeRiskCaseRepository.save(entity);
    }
}
